package com.lifesaftypower.entity;

import android.content.res.AssetManager;
import com.lifesaftypower.MainApp;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * User: Yang Sun
 * Date: 1/17/13
 * Time: 11:33 AM
 */
public class Product {
    private static List<Product> _productList;
    private String _model;
    private String _sector;
    private String _name;
    private String _description;
    private String _spec;
    private String _overview;
    private String _document;
    private String _support;
    private String _tool;

    public Product(Node xmlNode) {
        if (xmlNode.getNodeType() == Node.ELEMENT_NODE) {
            Element elem = (Element) xmlNode;
            _model = elem.getAttribute("model");
            _sector = elem.getAttribute("sector");
            _name = elem.getAttribute("name");
            _description = elem.getAttribute("description");


            _spec = getNodeValue(elem, "spec");
            _overview = getNodeValue(elem, "overview");
            _support = getNodeValue(elem, "support");
            _document = getNodeValue(elem, "document");

        }
    }

    public static Product[] getBySector(String marketSector) {
        ArrayList<Product> productList = new ArrayList<Product>();
        for (Product p : getProductList()) {
            if (p.getMarketSector().equals(marketSector)) {
                productList.add(p);
            }
        }
        return productList.toArray(new Product[]{});
    }

    public static String[] getSector() {
        HashSet<String> marketSectorSet = new HashSet<String>();
        for (Product p : getProductList()) {
            marketSectorSet.add(p.getMarketSector());
        }

        return marketSectorSet.toArray(new String[]{});
    }

    public static List<Product> getProductList() {
        if (_productList == null) {
            _productList = Load();
        }
        return _productList;
    }

    private static List<Product> Load() {
        AssetManager assetManager = MainApp.getAppContext().getAssets();
        ArrayList<Product> result = new ArrayList<Product>();
        try {
            InputStream ins = assetManager.open("product.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder dBuilder;

            dBuilder = dbFactory.newDocumentBuilder();

            Document doc = dBuilder.parse(ins);
            NodeList products = doc.getElementsByTagName("product");

            for (int i = 0; i < products.getLength(); i++) {
                Node productNode = products.item(i);
                Product product = new Product(productNode);
                result.add(product);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    private static String getNodeValue(Element elem, String nodeName) {
        String result = "";
        NodeList valueNodes = elem.getElementsByTagName(nodeName);
        if (valueNodes != null && valueNodes.getLength() > 0) {
            NodeList cdNodes = valueNodes.item(0).getChildNodes();

            for (int i = 0; i < cdNodes.getLength(); i++) {
                if (Node.CDATA_SECTION_NODE == cdNodes.item(i).getNodeType()) {
                    result = cdNodes.item(i).getNodeValue();
                    break;
                }
            }
        }
        return result;
    }

    public static Product getByModel(String model) {
        for (Product product : getProductList()) {
            if (product.getModel().equals(model)) {
                return product;
            }
        }
        return null;
    }

    public String getSpec() {
        return _spec;
    }

    public void setSpec(String spec) {
        _spec = spec;
    }

    public String getDescription() {
        return _description;
    }

    public void setDescription(String description) {
        _description = description;
    }

    public String getDocument() {
        return _document;
    }

    public String getMarketSector() {
        return _sector;
    }

    public void setMarketSector(String marketSector) {
        _sector = marketSector;
    }

    public String getModel() {
        return _model;
    }

    public void setModel(String model) {
        _model = model;
    }

    public String getName() {
        return _name;
    }

    public void setName(String name) {
        _name = name;
    }

    public String getOverview() {
        return _overview;
    }

    public void setOverview(String overview) {
        _overview = overview;
    }

    public String getSupport() {
        return _support;
    }

    public void setSupport(String support) {
        _support = support;
    }

    public String getTools() {
        return _tool;
    }

    public void setTools(String tools) {
        _tool = tools;
    }

    public void setDocuments(String documents) {
        _document = documents;
    }
}
